﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Shapes;
using Silvermoon.OpenGL;
using System.IO;
using System.Drawing;
using Silvermoon.Core;
using Silvermoon.OpenGL.Native;

namespace Silvermoon.Shapes
{
    public class ImageShape : ColorShape
    {
        private short[] rectangleVertex;

        protected uint BufferName { get; private set; }
        protected Texture Texture { get; private set; }

        protected short[] RectangleVertex { get { return rectangleVertex; } }

        public Size TextureSize { get { return Texture != null ? Texture.Size : Size.Empty; } }

        protected override RenderStyle StyleCore { get { return RenderStyle.Simple; } }

        protected override Texture TextureCore { get { return Texture; } }

        public ImageShape()
            : base()
        {
            EnableGlesBuffer = true;
        }

        public override void Dispose()
        {
            if (BufferName != 0) gl.DeleteBuffer(BufferName);
            BufferName = 0;
            base.Dispose();
        }


        /// <summary>
        /// Gets or sets the TextureElement that holds the image.
        /// </summary>
        public virtual Texture Image
        {
            get { return Texture; }
            set
            {
                if (Texture != value)
                {
                    Texture = value;
                    Invalidate();
                }
            }
        }

        public bool UseTextColor { get; set; }

        protected override Color GetShapeColor()
        {
            if (UseTextColor) return Screen.Current.Renderer.TextColor;
            return Color;
        }

        protected override void Layout()
        {
            if (Texture != null)
            {
                if (BufferName == 0) BufferName = gl.GenBuffer();

                rectangleVertex = VertexUtil.SquaredArray(Size);

                gl.BindBuffer(BufferTarget.ArrayBuffer, BufferName);
                gl.BufferData(BufferTarget.ArrayBuffer, sizeof(short) * 8, rectangleVertex, BufferUsage.StaticDraw);
                gl.BindBuffer(BufferTarget.ArrayBuffer, 0);

            }
            base.Layout();
        }

        public override void Render(Renderer renderer)
        {
            if (Texture != null)
            {
                Color c = GetShapeColor();
                gl.Color4x(c.R << 8, c.G << 8, c.B << 8, c.A * renderer.Alpha);

                Texture.Render();
                gl.BindBuffer(BufferTarget.ArrayBuffer, BufferName);
                gl.CheckError();
                gl.VertexPointer(2, glValueType.Short, 0, IntPtr.Zero);
                //gl.VertexPointer(2, glValueType.Short, 0, rectangleVertex);
                gl.CheckError();
                gl.DrawArrays(PrimitiveType.TriangleStrip, 0, rectangleVertex.Length / 2);
                gl.CheckError();
                gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
                gl.CheckError();
            }
        }

        protected override Shape CreateClone()
        {
            return new ImageShape();
        }

        protected override void Copy(Shape shape)
        {
            base.Copy(shape);
            ImageShape s = shape as ImageShape;
            s.Image = Image;
            s.UseTextColor = UseTextColor;
        }

        protected override void InvalidateColor()
        {
            if (!Color.IsEmpty) UseTextColor = false;
            base.InvalidateColor();
        }
    }
}
